package com.example.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.ws.rs.PathParam;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.example.auxiliar.SortCreator;
import com.example.dtos.ProductDTO;
import com.example.dtos.UsersDTO;
import com.example.exceptions.MissingAttributesException;
import com.example.exceptions.WrongDirectionSpecificationException;
import com.example.exceptions.WrongFormatException;
import com.example.models.ItemToPurchase;
import com.example.models.UserRole;
import com.example.models.Users;
import com.example.services.UsersService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

//@Api(value="users")
@RestController
@RequestMapping("/users")
public class UsersController {
	
	@Autowired
	UsersService usersService;
	
	@Autowired
	private Mapper mapper;
	
	Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
	SortCreator sortCreator = new SortCreator();

//	@ApiOperation(value="Register user")
	@RequestMapping(value="/register", method=RequestMethod.POST)
	@ResponseStatus(value= HttpStatus.CREATED)
	public void register(@RequestBody UsersDTO usersDTO){
		Users newUser= checkAndCreateUsers(usersDTO); 
		usersService.register(newUser);
	}	
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	@ResponseStatus(value= HttpStatus.ACCEPTED)
	public void login(@RequestBody UsersDTO usersDTO){
		Users newUser= checkAndCreateUsers(usersDTO); 
		usersService.login(newUser);
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ResponseStatus(value= HttpStatus.OK)
	  public Collection<UsersDTO> findAll(
			  @PathParam(value = "page") Integer page,
			  @PathParam(value = "size") Integer size,
			  @RequestParam(value = "sort", required = false) String sort){
		 Sort sortObject = sortCreator.getSortObject(sort);
		 Collection<UsersDTO> result= new ArrayList<UsersDTO>();
		 usersService.findUsers(page,size,sortObject).stream().forEach((u)->{result.add(mapper.map(u,UsersDTO.class));});
		 return result;
	 }
	
	//AUXILIAR METHODS
	 private Users checkAndCreateUsers(UsersDTO usersDTO) throws MissingAttributesException{
		 Set<ConstraintViolation<UsersDTO>> constraintViolations = validator.validate(usersDTO);
		 if(!(constraintViolations.isEmpty())){
			 String message="";
			 constraintViolations.stream().forEach(msg -> message.concat(msg.getMessage()));
			 throw new WrongFormatException(message);
		 }
		 return mapper.map(usersDTO,Users.class);
	 }

}
